package com.itheima.controller;

import com.itheima.pojo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class DemoCookieController {

    @GetMapping("/c1")
    public Result c1(HttpServletResponse response){
        response.addCookie(new Cookie("name","tom"));
        response.addCookie(new Cookie("age", "20"));

        return Result.success();
    }

    @GetMapping("/c2")
    public Result c2(HttpServletRequest request){
        //获取本次请求头中所有的cookie
        Cookie[] cookies = request.getCookies();
        //如果不为空，就遍历所有Cookie
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                //获取每个Cookie的名称和值，打印出来
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println("获取到Cookie数据：名称="+name+"，值="+value);
            }
        }

        return Result.success();
    }
}